function ChemicalsController($scope,$state,Notification,FileUploader,ChemicalService,context){

	$scope.companyUuid = context.getEmployee().company.uuid;
	
	$scope.cleaningServices = [
	                       	{name:'Select Cleaning Service',value:0},
	                       	{name:'Facade Cleaning',value:1},
	                   		{name:'Floor Cleaning',value:2},
	                   		{name:'Office Furniture ',value:3},
	                   		{name:'Washroom Cleaning',value:4}
	                   		
	                       ];
	
	//Default Selection
	$scope.cleaningService = $scope.cleaningServices[0];
	
	$scope.chemicalsList = [];
	
	
	$scope.submitChemical = function(){
		
		var data = 	"cleaningService="+$scope.cleaningService.name+"&" +
					"cleaningArea="+$scope.cleaningArea+"&" +
					"application="+$scope.application+"&" +
					"proposedBrand="+$scope.proposedBrand+"&" +
					"proposedChemical="+$scope.proposedChemical+"&" +
					"dilutionRatio="+$scope.dilutionRatio+"&" +
					"areaCoverage="+$scope.areaCoverage+"&" +
					"origin="+$scope.origin+"&" +
					"certification="+$scope.certification+"&" +
					"packing="+$scope.packing+"&" +
					"totalAreaCoverage="+$scope.totalAreaCoverage+"&" +
					"companyUuid="+$scope.companyUuid;
		
		var save = ChemicalService.save(data);
		
		save.$promise.then(
				function(data){
					if(data.meta.code == 200){
						console.log("Data:",data);
						Notification.success({message: 'Record saved successfully.', title: 'Success'});
						for(index in data.data){
							$scope.chemicalsList.push(data.data[index]);
						}
					}
					else{
						Notification.error({message:ErrorUtils.getMessageByErrorCode(data.meta.code), title: 'Error'});
					}
					
				},
				function(error){					
					Notification.error({message: "Some error occurred. Please try again later.", title: 'Error'});
				});
	};
	
	$scope.getChemicalsList = function(){
		$scope.chemicalsList = [];
		var get = ChemicalService.get({id:$scope.companyUuid});
		
		get.$promise.then(
				function(data){
					if(data.meta.code == 200){
						console.log("Data:",data);
						for(index in data.data){
							$scope.chemicalsList.push(data.data[index]);
						}
					}
					else{
						Notification.error({message:ErrorUtils.getMessageByErrorCode(data.meta.code), title: 'Error'});
					}
					
				},
				function(error){
					Notification.error({message: "Some error occurred. Please try again later.", title: 'Error'});
				});
	}
	
	$scope.deleteChemical = function(uuid,index){
		var del = ChemicalService.delete({id:uuid});
		
		del.$promise.then(
				function(data){
					if(data.meta.code == 200){
						console.log("Data:",data);
						$scope.deleteItemFromChemicalsList(index);
					}
					else{
						Notification.error({message:ErrorUtils.getMessageByErrorCode(data.meta.code), title: 'Error'});
					}
					
				},
				function(error){
					Notification.error({message: "Some error occurred. Please try again later.", title: 'Error'});
				});
	}
	
	$scope.deleteItemFromChemicalsList = function(index){
		var from 	= index;
		var to		= 0;
		var rest = $scope.chemicalsList.slice((to || from) + 1 || $scope.chemicalsList.length);
		$scope.chemicalsList.length = from < 0 ? $scope.chemicalsList.length + from : from;
		$scope.chemicalsList.push.apply($scope.chemicalsList, rest);
	}
	
	
	
	$scope.getChemicalsList();
}


angular.module('supplier.chemicals',['ngAnimate','ui.router','ui-notification','angularFileUpload'])
	.controller('ChemicalsController',['$scope','$state','Notification','FileUploader','ChemicalService','context',ChemicalsController]);